<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理 - 电影院管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f5f5;
            line-height: 1.6;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .nav {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
        }
        
        .logo {
            font-size: 24px;
            font-weight: bold;
        }
        
        .nav-links {
            display: flex;
            gap: 30px;
        }
        
        .nav-links a {
            color: white;
            text-decoration: none;
            font-weight: 500;
            transition: opacity 0.3s;
        }
        
        .nav-links a:hover {
            opacity: 0.8;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .users-section {
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        
        .section-title {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }
        
        .search-bar {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
            align-items: center;
        }
        
        .search-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        
        .users-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        .users-table th,
        .users-table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #e1e1e1;
        }
        
        .users-table th {
            background-color: #f8f9fa;
            font-weight: bold;
            color: #333;
        }
        
        .users-table tr:hover {
            background-color: #f8f9fa;
        }
        
        .user-role {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .role-admin {
            background-color: #fff3cd;
            color: #856404;
        }
        
        .role-user {
            background-color: #d4edda;
            color: #155724;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-size: 12px;
            transition: transform 0.2s;
            margin-right: 5px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
        }
        
        .btn-sm {
            padding: 6px 12px;
            font-size: 12px;
        }
        
        .btn-danger {
            background: #dc3545;
        }
        
        .btn-warning {
            background: #ffc107;
            color: #333;
        }
        
        .btn-success {
            background: #28a745;
        }
        
        .btn-secondary {
            background: #6c757d;
        }
        
        .no-data {
            text-align: center;
            color: #666;
            font-style: italic;
            padding: 40px;
        }
        
        .alert {
            padding: 10px;
            margin-bottom: 20px;
            border-radius: 5px;
            text-align: center;
        }
        
        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        
        .modal-content {
            background-color: white;
            margin: 15% auto;
            padding: 20px;
            border-radius: 10px;
            width: 80%;
            max-width: 500px;
        }
        
        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }
        
        .close:hover {
            color: black;
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        
        .form-group input,
        .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        
        .stats-cards {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
        }
        
        .stat-number {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .stat-label {
            font-size: 14px;
            opacity: 0.9;
        }
    </style>
</head>
<body>
    <header class="header">
        <nav class="nav">
            <div class="logo">🎬 管理员控制台</div>
            <div class="nav-links">
                <a href="/admin/dashboard">控制台</a>
                <a href="/admin/users">用户管理</a>
                <a href="/admin/rooms">放映厅管理</a>
                <a href="/admin/logs">系统日志</a>
            </div>
            <div class="user-info">
                <span th:text="'管理员：' + ${currentUser != null ? currentUser.username : '未登录'}">管理员</span>
                <a href="/auth/logout" class="btn btn-sm">退出登录</a>
            </div>
        </nav>
    </header>
    
    <div class="container">
        <div class="users-section">
            <h2 class="section-title">用户管理</h2>
            
            <!-- 统计卡片 -->
            <div class="stats-cards">
                <div class="stat-card">
                    <div class="stat-number" th:text="${totalUsers ?: 0}">0</div>
                    <div class="stat-label">总用户数</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number" th:text="${adminUsers ?: 0}">0</div>
                    <div class="stat-label">管理员数</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number" th:text="${managerUsers ?: 0}">0</div>
                    <div class="stat-label">经理数</div>
                </div>
                <div class="stat-card">
                    <div class="stat-number" th:text="${receptionistUsers ?: 0}">0</div>
                    <div class="stat-label">前台数</div>
                </div>
            </div>
            
            <!-- 消息提示 -->
            <div th:if="${success}" class="alert alert-success" th:text="${success}"></div>
            <div th:if="${error}" class="alert alert-error" th:text="${error}"></div>
            <div th:if="${successMessage}" class="alert alert-success" th:text="${successMessage}"></div>
            <div th:if="${errorMessage}" class="alert alert-error" th:text="${errorMessage}"></div>
            
            <!-- 搜索栏 -->
            <div class="search-bar">
                <input type="text" class="search-input" placeholder="搜索用户名或手机号..." 
                       id="searchInput" onkeyup="filterUsers()">
                <button class="btn" onclick="openAddUserModal()">添加用户</button>
            </div>
            
            <!-- 用户表格 -->
            <div th:if="${users != null and !users.isEmpty()}">
                <table class="users-table" id="usersTable">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>用户名</th>
                            <th>手机号码</th>
                            <th>用户类型</th>
                            <th>注册时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                        <tbody>
                            <tr th:each="user : ${users}">
                                <td th:text="${user.userId}">ID</td>
                                <td th:text="${user.username}">用户名</td>
                                <td th:text="${user.phoneNumber}">手机号码</td>
                                <td>
                                    <span class="user-role" 
                                          th:classappend="${user.role == 'admin' ? 'role-admin' : (user.role == 'manager' ? 'role-manager' : 'role-receptionist')}"
                                          th:text="${user.role == 'admin' ? '管理员' : (user.role == 'manager' ? '经理' : '前台')}">用户类型</span>
                                </td>
                                <td th:text="${#temporals.format(user.registrationTime, 'yyyy-MM-dd HH:mm')}">注册时间</td>
                                <td>
                                    <button class="btn btn-warning" 
                                            th:data-user-id="${user.userId}"
                                            onclick="editUser(this.dataset.userId)">编辑</button>
                                    <button class="btn btn-danger" 
                                            th:if="${currentUser != null and user.userId != currentUser.userId and user.username != 'admin'}"
                                            th:data-user-id="${user.userId}"
                                            th:data-username="${user.username}"
                                            onclick="deleteUser(this.dataset.userId, this.dataset.username)">删除</button>
                                </td>
                            </tr>
                        </tbody>
                </table>
            </div>
            
            <div th:if="${users == null or users.isEmpty()}" class="no-data">
                <p>暂无用户数据</p>
            </div>
        </div>
    </div>
    
    <!-- 添加用户模态框 -->
    <div id="addUserModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeAddUserModal()">&times;</span>
            <h3>添加新用户</h3>
            <form action="/admin/users/add" method="post">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <div class="form-group">
                    <label for="phoneNumber">手机号码</label>
                    <input type="tel" id="phoneNumber" name="phoneNumber" 
                           pattern="1[0-9]{10}" 
                           title="请输入11位手机号码，以1开头" 
                           maxlength="11" 
                           required>
                    <small style="color: #666; font-size: 12px;">请输入11位手机号码，以1开头</small>
                </div>
                <div class="form-group">
                    <label for="role">用户类型</label>
                    <select id="role" name="role">
                        <option value="manager">经理</option>
                        <option value="admin">管理员</option>
                        <option value="receptionist">前台</option>
                    </select>
                </div>
                <button type="submit" class="btn">添加用户</button>
                <button type="button" class="btn btn-secondary" onclick="closeAddUserModal()">取消</button>
            </form>
        </div>
    </div>
    
    <script>
        function filterUsers() {
            const input = document.getElementById('searchInput');
            const filter = input.value.toLowerCase();
            const table = document.getElementById('usersTable');
            const rows = table.getElementsByTagName('tr');
            
            for (let i = 1; i < rows.length; i++) {
                const cells = rows[i].getElementsByTagName('td');
                const username = cells[1].textContent.toLowerCase();
                const phone = cells[2].textContent.toLowerCase();
                
                if (username.includes(filter) || phone.includes(filter)) {
                    rows[i].style.display = '';
                } else {
                    rows[i].style.display = 'none';
                }
            }
        }
        
        function openAddUserModal() {
            document.getElementById('addUserModal').style.display = 'block';
        }
        
        function closeAddUserModal() {
            document.getElementById('addUserModal').style.display = 'none';
            // 清空表单
            document.querySelector('#addUserModal form').reset();
            // 清除验证提示
            const phoneInput = document.getElementById('phoneNumber');
            phoneInput.style.borderColor = '';
            const errorMsg = document.getElementById('phoneError');
            if (errorMsg) errorMsg.remove();
        }
        
        // 手机号实时验证
        document.addEventListener('DOMContentLoaded', function() {
            const phoneInput = document.getElementById('phoneNumber');
            
            phoneInput.addEventListener('input', function() {
                const phone = this.value;
                const isValid = /^1[0-9]{10}$/.test(phone);
                
                // 移除之前的错误提示
                const existingError = document.getElementById('phoneError');
                if (existingError) existingError.remove();
                
                if (phone.length > 0 && !isValid) {
                    this.style.borderColor = '#ff4444';
                    const errorMsg = document.createElement('small');
                    errorMsg.id = 'phoneError';
                    errorMsg.style.color = '#ff4444';
                    errorMsg.style.fontSize = '12px';
                    errorMsg.textContent = '手机号格式不正确，请输入11位数字，以1开头';
                    this.parentNode.appendChild(errorMsg);
                } else {
                    this.style.borderColor = phone.length === 11 && isValid ? '#4CAF50' : '';
                }
            });
            
            // 只允许输入数字
            phoneInput.addEventListener('keypress', function(e) {
                if (!/[0-9]/.test(e.key) && e.key !== 'Backspace' && e.key !== 'Delete' && e.key !== 'Tab') {
                    e.preventDefault();
                }
            });
        });
        
        function editUser(userId) {
            window.location.href = '/admin/users/edit/' + userId;
        }
        

        
        
        function deleteUser(userId, username) {
            if (username === 'admin') {
                alert('不能删除admin管理员账户！');
                return;
            }
            
            if (confirm('确定要删除用户 ' + username + ' 吗？此操作不可恢复！\n\n删除用户将会：\n- 删除用户的所有个人信息\n- 删除用户的操作记录\n- 此操作将被记录到系统日志')) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = '/admin/users/delete';
                
                const userIdInput = document.createElement('input');
                userIdInput.type = 'hidden';
                userIdInput.name = 'userId';
                userIdInput.value = userId;
                
                form.appendChild(userIdInput);
                document.body.appendChild(form);
                form.submit();
            }
        }
        
        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('addUserModal');
            if (event.target === modal) {
                modal.style.display = 'none';
            }
        }
    </script>
</body>
</html>